package com.example.job;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalTime;

@Slf4j
@Component
public class MyQuartzJob extends QuartzJobBean {

    @Autowired
    private Scheduler scheduler;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        // 在这里编写你的任务内容
        log.info("定时任务进来了....");

        // 检查是否满足关闭定时任务的条件
        if (LocalTime.now().isAfter(LocalTime.of(5, 0))) {
            try {
                // 取消定时任务
                scheduler.unscheduleJob(context.getTrigger().getKey());
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }
}